<template>
	<view>
		<u-toast ref="uToast" /><u-no-network></u-no-network>
		<u-navbar title="商品列表" safeAreaInsetTop fixed placeholder>
		    <view class="coreshop-navbar-left-slot" slot="left">
		        <u-icon name="arrow-left" size="19" @click="goNavigateBack"></u-icon>
		        <u-line direction="column" :hairline="false" length="16" margin="0 8px"></u-line>
		        <u-icon name="home" size="22" @click="goHome"></u-icon>
		    </view>
		    <view slot="right">
		    </view>
		</u-navbar>
		<view class="content">
			<view class="coreshop-padding-10 coreshop-margin-bottom-5 coreshop-bg-white coreshop-solid-bottom">
			    <u-search placeholder="请输入商品名称关键字搜索" v-model="key" :show-action="true" action-text="搜索" :animation="false" @custom="doSearch()"></u-search>
			</view>
			<scroll-view class="scroll-box" scroll-y>
				<view v-if="list.length">
					<view class="item coreshop-display-flex coreshop-flex-nowrap coreshop-justify-between" v-for="(item, key) in list" :key="key">
						<view class="c-m-r-10">
							<image class='itmImg' mode="aspectFill" :src="item.image"></image>
						</view>
						<view class="coreshop-display-flex coreshop-flex-sub coreshop-flex-direction">
							<view class="top">
								<view class="name">{{ item.name }}</view>
								<view class="price">¥{{ item.price }}</view>
							<!-- <view class="tag">
									<text class="red" v-if="item.isDefault">默认</text>
								</view> -->
							</view>
							<view class="bottom">
								<view class="coreshop-display-flex"><u--text text="分类:"></u--text>{{item.goodsCategoryName || ''}}</view>
								<view class="coreshop-display-flex"><u--text text="品牌:"></u--text> {{item.brandName || ''}}  </view>
							</view>
						</view>
						<view><u-icon name="edit-pen-fill" :size="20" color="#3191fd" @click="toEdit(item)"></u-icon></view>
					</view>
					<!-- 更多 -->
					<u-loadmore :status="loadStatus" :icon-type="iconType" :load-text="loadText" margin-top="20" margin-bottom="20" />
				</view>
				<!-- 无数据时默认显示 -->
				<view class="coreshop-emptybox" v-else>
					<u-empty :icon="$globalConstVars.apiFilesUrl+'/static/images/empty/address.png'" icon-size="150" text="暂无商品信息" mode="list"></u-empty>
				</view>
			</scroll-view>
			
			<!--按钮-->
			<!-- <view class="coreshop-bg-white coreshop-footer-fixed coreshop-foot-padding-bottom">class="floatingButton" -->
			<view class="floatingButton" @click="toAdd">
			    <!-- <u-button class="coreshop-percent-100" type="error" size="normal" @click="toAdd">新增商品</u-button> -->
				 <u-icon name="plus-circle-fill" color="#e54d42" size="45"></u-icon>
			</view>
		</view>
	</view>
</template>

<script>
	export default{
		data() {
		    return {
		        list: [],// 列表
				dealerId:'',  
				loadStatus: 'loadmore', //loadmore-加载前的状态，loading-加载中的状态，nomore-没有更多的状态
				iconType: 'flower',
				loadText: {
				    loadmore: '轻轻上拉',
				    loading: '努力加载中',
				    nomore: '实在没有了'
				},
				currentPage: 1, 
				key: '',
				searchData: {
					id:null,
				    where: {},
				    limit: 10,
				    page: 1 
				},
		    }
		},
		onShow() {
			this.currentPage=1;
			this.list=[];
			this.getDealerGoods();
		},
		onReachBottom() {// 加载更多
		    if (this.loadStatus === 'loadmore') {
		        this.getDealerGoods()
		    }
		},
		//加载执行
		onLoad: function (options) {
		    this.dealerId = options.store;  
		},
		methods:{ 
			//获取列表
			getDealerGoods(){
				let self=this;
				 
				this.loadStatus = 'loading';
				this.$u.api.getDealerGoods(this.conditions()).then(res => {
				    if (res.status) {
				        let _list = res.data;
						self.list = [...self.list, ..._list]
						let brands=res.otherData;
						self.list.forEach((item,index)=>{
						  let br=	brands.find(c=>c.id==item.brandId) ;
						  if(br!=null)
						  item.brandName=br.name;
						});	
						
						if (res.code > self.list.length) {
						    self.currentPage++
						    self.loadStatus = 'loadmore'
						} else {
						    self.loadStatus = 'nomore'
						}
				    }
				})
			},
			//添加
			toAdd() {
				let store={};
				store.dealerId=this.dealerId;
			    this.$u.route('/pages/member/dealer/dealerGoods/index/index?store=' + JSON.stringify(store));
			},
			//编辑
			toEdit(item) {
				let store=item;
				store.dealerId=this.dealerId;
			    this.$u.route('/pages/member/dealer/dealerGoods/index/index?store=' + JSON.stringify(store));
			},
			//搜索
			doSearch() {
				this.currentPage=1;
				this.list=[];
				this.getDealerGoods();
			},
			// 统一返回筛选条件 查询条件 分页
			conditions() {
				var where = {};
				if(this.key != ''){
					where = {
					    searchName: this.key
					};
				}
				this.searchData.where = where;
				this.searchData.id = this.dealerId;
				this.searchData.page = this.currentPage;
				
			    let data = this.searchData;
			    var newData = this.$u.deepClone(data);
			    if (data.where) {
			        newData.where = JSON.stringify(data.where);
			    } 
			    return newData;
			},
		}
	}
</script>

<style lang="scss" scoped>
	.item { padding: 20px 20px 20px 5px; background: #fff; margin: 5px 10px;border-radius:8px;
	    .top { display: flex; font-weight: bold; font-size: 17px;
	        .price { margin-left: 30px;color: red; }
	        .tag { display: flex; font-weight: normal; align-items: center;
	            text { display: block; width: 30px; height: 17px; line-height: 17px; color: #ffffff; font-size: 10px; border-radius: 3px; text-align: center; margin-left: 15px; background-color: #3191fd; }
	            .red { background-color: red; }
	        }
	    }
	    .bottom { display: flex; margin-top: 10px; font-size: 14px; justify-content: space-between; color: #999999; }
	}
	
	.itmImg{
		width: 90rpx;
		height: 90rpx;
	}
</style>